package chaw.android.train.MoreActivities;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import chaw.android.train.AllBoardActivity;
import chaw.android.train.BoardViewActivity;
import chaw.android.train.FavoriteActivity;
import chaw.android.train.MoreActivity;
import chaw.android.train.R;
import chaw.android.train.TrainTravelActivity;
import chaw.android.util.HttpRequestManager;
import chaw.android.util.Preferences;
import chaw.android.util.ServerURL;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

public class MoreNoticeActivity extends Activity{
	private WebView moreNoticeWebView;
	private WebViewClient client;
	private ImageView backBtn;
	private WebView bannerWebView;
	private WebViewClient webViewClient;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
        setContentView(R.layout.more_notice);
        
        bannerWebView = (WebView) findViewById(R.id.banner);
	    webViewClient = new WebViewClient();
//		bannerWebView.setWebViewClient(webViewClient);
//		bannerWebView.loadUrl(ServerURL.bannerWebView);
	    bannerWebView.setWebViewClient(new WebViewClient(){
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url){
				String[] values = url.split("!");
				if(values[1].equals("app")){
					if(values[2].equals("live")){
						Intent intent = new Intent(getBaseContext(), AllBoardActivity.class);
						startActivity(intent);
						
					}
					else if(values[2].equals("enjoy")){
						Intent intent = new Intent(getBaseContext(), MoreSpecialActivity.class);
						startActivity(intent);
					}
				}
				else if(values[1].equals("board")){
					StationIdAsynkTask task = new StationIdAsynkTask();
					String[] params = new String[]{ ServerURL.stationIdFromBoard, "board_id", values[2]};
					task.execute(params);
				}
				else if(values[1].equals("web")){
					Intent intent = new Intent(Intent.ACTION_VIEW); 
					Uri uri = Uri.parse(values[2]); 
					intent.setData(uri); 
					startActivity(intent);
				}
				return true;
			}
		});
		bannerWebView.loadUrl(ServerURL.bannerWebView(Preferences.getValue(getBaseContext(), "device_token"),
													  Preferences.getValue(getBaseContext(), "user_id"),
													  Preferences.getValue(getBaseContext(), "version")));
		bannerWebView.getSettings().setJavaScriptEnabled(true);
		
        backBtn = (ImageView) findViewById(R.id.backBtn);
        backBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				MoreActivity parentActivity = (MoreActivity)getParent();
				parentActivity.finishFromChild(MoreNoticeActivity.this);
			}
		});
        
        moreNoticeWebView = (WebView) findViewById(R.id.moreNoticeWebView);
        client = new WebViewClient();
		String url = ServerURL.moreNoticeWebView;
		moreNoticeWebView.setWebViewClient(client);
		moreNoticeWebView.loadUrl(url);
		moreNoticeWebView.getSettings().setJavaScriptEnabled(true);
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		for(int i = 0 ; i < 5; i++)
        	TrainTravelActivity.tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.bg_nav1);
	}
	
	private class StationIdAsynkTask extends AsyncTask<String, Void, String> {
		@Override
		protected String doInBackground(String... urls) {
			// TODO Auto-generated method stub
			String response = null;
			
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			
			if(urls.length > 1)
				for(int i = 1; i< urls.length; i+=2)
					params.add(new BasicNameValuePair(urls[i], urls[i+1]));
			response = HttpRequestManager.request(urls[0], params);
			return response;
		}
		
		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			try {
				JSONObject station = new JSONObject(result);
				String station_id = station.get("station_id").toString();
				String station_name = station.get("station_name").toString();
				String board_id = station.get("board_id").toString();
				Intent intent = new Intent(getBaseContext(), BoardViewActivity.class);
				intent.putExtra("board_id", board_id);
				intent.putExtra("station_id", station_id);
				intent.putExtra("station_name", station_name);
				TrainTravelActivity tab = (TrainTravelActivity) getParent();
		        TrainTravelActivity.tabHost.setCurrentTab(3);
				tab.onTabChanged("Tab4");
				((FavoriteActivity) tab.getCurrentActivity()).startChildActivity("BoardView", intent);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}